package com.sopt.eat.views;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Align;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.sopt.eat.R;

public class CenterPointOverlay extends Overlay {
	private GeoPoint geoPoint;
	private Location currentLocation;
	
	public CenterPointOverlay(Location location) {
		currentLocation = location;
		Double lat = currentLocation.getLatitude() * 1E6;
		Double lot = currentLocation.getLongitude() * 1E6;
		setGeopoint(new GeoPoint(lat.intValue(),lot.intValue()));		
	}
	
	private void setGeopoint(GeoPoint geoPoint) {
		this.geoPoint = geoPoint;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		
		super.draw(canvas, mapView, shadow);
		
		Point locationPoint = new Point();
		Projection projection = mapView.getProjection();
		projection.toPixels(getGeopoint(), locationPoint);
		
		
		
		Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		linePaint.setStyle(Paint.Style.STROKE);
		linePaint.setStrokeWidth(1);
		linePaint.setColor(Color.BLUE);
		
		Paint firstTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		firstTextPaint.setColor(Color.BLUE);
		firstTextPaint.setTextSize(20);
		
		firstTextPaint.setTextAlign(Align.CENTER);
		
		Paint fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		fillPaint.setColor(Color.BLUE);
		fillPaint.setAlpha(35);
		
		int containerX =locationPoint.x;
		int containerY = locationPoint.y;
		
		
		
		GeoPoint radius = projection.fromPixels(0, 0);
		
		Double searchWidth = (currentLocation.getLatitude() - radius.getLatitudeE6()/1E6);
		Double searchHeight = (currentLocation.getLongitude() - radius.getLongitudeE6()/1E6);
		
		if (shadow) {
			 Bitmap bmp = BitmapFactory.decodeResource( mapView.getContext().getResources(),R.drawable.arrowshadow );            
		            canvas.drawBitmap(bmp, containerX-23,containerY-bmp.getHeight(), null);
		} else {
			canvas.drawText("300m", containerX, containerY-projection.metersToEquatorPixels((float) 300.0), firstTextPaint);
			canvas.drawCircle(containerX, containerY, projection.metersToEquatorPixels((float) 300.0), linePaint);
			canvas.drawCircle(containerX, containerY, projection.metersToEquatorPixels((float) 300.0 - 1 ), fillPaint);
			
			
			canvas.drawText("600m", containerX, containerY-projection.metersToEquatorPixels((float) 600.0), firstTextPaint);
			linePaint.setAlpha(150);
			canvas.drawCircle(containerX, containerY, projection.metersToEquatorPixels((float) 600.0), linePaint);
			fillPaint.setAlpha(25);
			canvas.drawCircle(containerX, containerY, projection.metersToEquatorPixels((float) 600.0 - 1 ), fillPaint);
			
			
			Bitmap bmp = BitmapFactory.decodeResource( mapView.getContext().getResources(),R.drawable.arrow );
			
            canvas.drawBitmap(bmp, containerX-23,containerY-bmp.getHeight(), null);
		}
	}

	private GeoPoint getGeopoint() {
		// TODO Auto-generated method stub
		return geoPoint;
	}
}
